package com.smbms.controller;

import com.alibaba.fastjson.JSON;
import com.smbms.pojo.Bill;
import com.smbms.pojo.Provider;
import com.smbms.pojo.User;
import com.smbms.servlet.ProviderService;
import com.smbms.util.PageBean;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("/provider")
public class ProviderController {


    @Resource
    private ProviderService providerService;

    @RequestMapping("/delete/{proid}")
    @ResponseBody //加上这个注释就是ajax请求
    public String delete(@PathVariable Integer proid){
        Map<String,Object> map = new HashMap<String,Object>();
        Bill bill = providerService.selectBillById(proid);
        if(bill != null){
            map.put("delResult","noNull");
        }else if(bill == null){
            int row = providerService.deleteProviderById(proid);
            if(row > 0){
                map.put("delResult","true");
            }else if(row == 0){
                map.put("delResult","notexist");
            }else {
                map.put("delResult","false");
            }
        }
        return JSON.toJSONString(map);
    }

    @RequestMapping("/modifysave")
    public String modifySave(@ModelAttribute Provider provider){
        int row = providerService.updateProvider(provider);
        if(row > 0){
            return "redirect:/provider/provider";
        }else {
            return "providermodify";
        }
    }

    @RequestMapping("/modify/{proid}")
    public String modify(@PathVariable Integer proid,HttpServletRequest request){
        Provider provider = providerService.getProviderById(proid);
        if(provider != null){
            request.setAttribute("provider",provider);
            return "providermodify";
        }
        return "redirect:/provider/provider";
    }

    @RequestMapping("/view/{proid}")
    public String providerView(@PathVariable Integer proid,HttpServletRequest request){
        Provider provider = providerService.getProviderById(proid);
        if(provider != null){
            request.setAttribute("provider",provider);
            return "providerview";
        }else {
            return "redirect:/provider/provider";
        }


    }

    @RequestMapping("provideraddsave")
    public String providerAddSave(HttpSession session, @ModelAttribute Provider provider){
        /*创建时间*/
        User loginUser = (User)session.getAttribute("loginUser");
        provider.setCreatedBy(loginUser.getId());
        provider.setCreationDate(new Date());
        boolean resout = providerService.addProvider(provider);
        if(resout){
            return "redirect:/provider/provider";
        }
        return "useradd";
    }

    @RequestMapping("/provideradd")
    public String providerAdd(){
        return "provideradd";
    }

    /*查询供应商控制器*/
    @RequestMapping("provider")
    public String provider(@RequestParam(defaultValue = "1") Integer pageIndex, HttpServletRequest request,String queryProCode,String queryProName){
        /*每页显示多少条数据*/
        int pageSize = 5;
        /*由于参数太多，我们存到map集合中去*/
        Map<String,Object> map = new HashMap<>();
        map.put("pageIndex",pageIndex);
        map.put("pageSize",pageSize);
        map.put("queryProCode",queryProCode);
        map.put("queryProName",queryProName);
        PageBean<Provider> page = providerService.getPage(map);
        request.setAttribute("page",page);

        /*做回显数据*/
        request.setAttribute("queryProCode",queryProCode);
        request.setAttribute("queryProName",queryProName);
        return "providerlist";


    }
}
